package game.map
{
	import game.manager.ConfigurationManager;
	import game.manager.MapManager;
	import starling.display.Sprite;
	import starling.events.Event;
	
	/**
	 * ...
	 * @author Kirill Pletnev
	 */
	public class VisualMap extends Sprite
	{		
		private var _numColumns:int;
		private var _numRows:int;
		private var _gameCamera:GameCamera;
		private var _backgraundContainer:BackgraundContainer;
		private var _tilesContainer:MapTilesContainer;
		private var _gameObjects:Sprite;
		//====================== Temp vars ============================
		private var _mapWidth:int;
		private var _mapHeigth:int
		private var _mapHalfWidth:int;
		private var _mapHalfHeigth:int;		
		//=============================================================
		
		public function VisualMap()
		{
			this.visible = false;
			this.touchable = true;			
			_numColumns = MapManager.instance.columnsMap;
			_numRows = MapManager.instance.rowsMap;
			_mapWidth = ConfigurationManager.instance.tileWidth * _numColumns;
			_mapHeigth = ConfigurationManager.instance.tileHeight * _numRows;
			_mapHalfWidth = _mapWidth / 2;
			_mapHalfHeigth = _mapHeigth / 2;
			this.x = _mapHalfWidth;
			this.y = _mapHalfHeigth;
			
			initBackgraund();
			initTilesLayer();
			initGameObjects();
			
			trace("mapWidth = " + _mapWidth);
			trace("mapHeigth = " + _mapHeigth);
		}
		
		private function initBackgraund():void
		{
			_backgraundContainer = new BackgraundContainer(this);
			_backgraundContainer.addEventListener(Event.COMPLETE, onInitMap);
			_backgraundContainer.initBackgraund();
			addChild(_backgraundContainer);
		}
		
		private function initTilesLayer():void
		{
			_tilesContainer = new MapTilesContainer(this);
			_tilesContainer.addEventListener(Event.COMPLETE, onInitMap);
			_tilesContainer.initMapTiles();
			addChild(_tilesContainer);
		}
		
		private function initGameObjects():void
		{
			_gameObjects = new Sprite();
			addChild(_gameObjects);
		}
		
		private function initGameCamera():void
		{
			_gameCamera = new GameCamera(this);
			_gameCamera.moveToCenter();
		}
		
		private function onInitMap(event:Event):void
		{
			if (_tilesContainer.isInit && _backgraundContainer.isInit)
			{
				initGameCamera();
				_backgraundContainer.removeEventListener(Event.COMPLETE, onInitMap);
				_tilesContainer.removeEventListener(Event.COMPLETE, onInitMap);
				
				this.visible = true;
				SplashScreen.instance.hide();
				trace("VisualMap initialized")
			}
		}
		
		public static function getColumn(x:int, y:int):int
		{
			return (x / ConfigurationManager.instance.tileWidth) + (y / ConfigurationManager.instance.tileHeight);
		}
		
		public static function getRow(x:int, y:int):int
		{
			return (y / ConfigurationManager.instance.tileHeight) - (x / ConfigurationManager.instance.tileWidth);
		}
		
		public static function getX(column:int, row:int):int
		{
			return (column - row) * ConfigurationManager.instance.tileHalfWidth;
		}
		
		public static function getY(column:int, row:int):int
		{
			return ((column + row) * ConfigurationManager.instance.tileHalfHeight);
		}
		
		public static function getCenterX(column:int, row:int):int
		{
			return getX(column, row);
		}
		
		public static function getCenterY(column:int, row:int):int
		{
			return getY(column, row) + (ConfigurationManager.instance.tileHeight / 2);
		}
		
		public function get gameCamera():GameCamera
		{
			return _gameCamera;
		}
		
		public function get mapWidth():int
		{
			return _mapWidth;
		}
		
		public function get mapHeigth():int
		{
			return _mapHeigth;
		}
		
		public function get mapHalfWidth():int
		{
			return _mapHalfWidth;
		}
		
		public function get mapHalfHeigth():int
		{
			return _mapHalfHeigth;
		}
		
		public function get numRows():int
		{
			return _numRows;
		}
		
		public function get numColumns():int
		{
			return _numColumns;
		}
		
		public function get tilesContainer():MapTilesContainer
		{
			return _tilesContainer;
		}
		
		public function get gameObjects():Sprite 
		{
			return _gameObjects;
		}
	
	}

}